import type { MockMethod } from 'vite-plugin-mock'

import type { requestParams } from './_util'
import { resultSuccess, resultError, getRequestToken } from './_util'

export function createFakeUserList() {
  return [
    {
      userId: '1',
      userInfo: {
        userName: 'admin',
        role: 'admin'
      },
      username: 'admin',
      password: '123456'
    },
    {
      userId: '2',
      userInfo: {
        userName: 'teacher',
        role: 'teacher'
      },
      username: 'teacher',
      password: '123456'
    },
    {
      userId: '3',
      userInfo: {
        userName: 'student',
        role: 'student'
      },
      username: 'student',
      password: '123456'
    }
  ]
}

export default [
  // mock user login
  {
    url: '/mock-api/login',
    timeout: 200,
    method: 'post',
    // @ts-ignore-next-line
    response: ({ body }) => {
      const { username, password } = body
      const checkUser = createFakeUserList().find(
        item => item.username === username && password === item.password
      )
      if (!checkUser) {
        return resultError('Incorrect account or password！')
      }
      const { userId } = checkUser
      return resultSuccess({ token: userId }) //把userId当token返回去
    }
  },
  {
    url: '/mock-api/info',
    method: 'get',
    response: (request: requestParams) => {
      const token = getRequestToken(request)
      if (!token) return resultError('No token')
      const checkUser = createFakeUserList().find(item => item.userId === token)
      if (!checkUser) {
        return resultError(
          'The corresponding user information was not obtained!'
        )
      }
      return resultSuccess(checkUser)
    }
  }
  // {
  //   url: '/basic-api/getPermCode',
  //   timeout: 200,
  //   method: 'get',
  //   response: (request: requestParams) => {
  //     const token = getRequestToken(request)
  //     if (!token) return resultError('Invalid token')
  //     const checkUser = createFakeUserList().find(item => item.token === token)
  //     if (!checkUser) {
  //       return resultError('Invalid token!')
  //     }
  //     const codeList = fakeCodeList[checkUser.userId]

  //     return resultSuccess(codeList)
  //   }
  // },
  // {
  //   url: '/basic-api/logout',
  //   timeout: 200,
  //   method: 'get',
  //   response: (request: requestParams) => {
  //     const token = getRequestToken(request)
  //     if (!token) return resultError('Invalid token')
  //     const checkUser = createFakeUserList().find(item => item.token === token)
  //     if (!checkUser) {
  //       return resultError('Invalid token!')
  //     }
  //     return resultSuccess(undefined, { message: 'Token has been destroyed' })
  //   }
  // }
] as MockMethod[]
